﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;
using DAO;
namespace BUS
{
    public class PhongBUS
    {

        public const int themPhongOK = 0;
        public const int trungTenPhong = 1;
        public const int vuotQuaSoPhong = 2;
        public const int themPhongLoi = 3;
        public const int suaPhongOK = 4;
        public const int suaPhongLoi = 5;

        public static int ThemPhong(PhongDTO phong)
        {
            if (LayDSPhong() != null)
            {
                if (LayDSPhong().Count == 50)// quy dinh khach san chi co toi da 50 phong
                    return vuotQuaSoPhong;
                if (LayDSPhong(-1, phong.TenPhong).Count > 0)
                    return trungTenPhong;
            }
            if (!PhongDAO.ThemPhong(phong))
                return themPhongLoi;
            else// neu them phong thanh cong thi chon dai 1 vi tri trong cho phong moi
            {
                for (int i = 0; i < 5; i++)
                    for (int j = 0; j < 10; j++)
                    {
                        if (!PhongDAO.KiemTraViTriPhongDaCoPhong(i , j))
                        {
                            phong.Dong = i;
                            phong.Cot = j;
                            PhongDAO.SuaViTriPhong(phong);
                            break;
                        }
                    }
                return themPhongOK;
            }
        }


        public static int SuaPhong(PhongDTO phong)
        {
            if (LayDSPhong(-1, phong.TenPhong).Count > 0 && LayDSPhong(-1, phong.TenPhong)[0].MaPhong  != phong.MaPhong)
                return trungTenPhong;
            if (!PhongDAO.SuaPhong(phong))
                return suaPhongLoi;
            return suaPhongOK;
        }
        public static bool SuaViTri(PhongDTO phong)
        {
            return PhongDAO.SuaViTriPhong(phong);
        }
        public static bool XoaPhong(int _maPhong)
        {
            if (PhongDAO.XoaPhong(_maPhong))// xoa thanh cong, xoa cac thanh phan lien quan
            {
                //xoa phieu thue phong
                List<PhieuThuePhongDTO> dsPhieuThue = PhieuThuePhongBUS.LayDSPhieuThuePhong(-1, -1, -1, -1,-1, _maPhong);
                if (dsPhieuThue != null && dsPhieuThue.Count > 0)
                    foreach (var item in dsPhieuThue)
                        if (!PhieuThuePhongBUS.XoaPhieuThuePhong(item.MaPhieuThuePhong))
                            return false;
                return true;
            }
            return false;
        }
        public static List<PhongDTO> LayDSPhong(int maPhong = -1, string tenPhong = null, int trangThai = -1, int maLoaiPhong = -1)
        {
            return PhongDAO.LayDSPhong(maPhong, tenPhong, trangThai,maLoaiPhong);
        }
    }
}
